﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Text;
using System.Threading.Tasks;

namespace SimpleReactiveObjects
{
    /// <summary>
    /// Provides a set of static methods for <see cref="ReactiveExpression{T}"/>.
    /// </summary>
    public static class ReactiveExpression
    {
        /// <summary>
        /// Converts an observable sequence to <see cref="ReactiveExpression{T}"/>.
        /// </summary>
        /// <typeparam name="T">The type of value.</typeparam>
        /// <param name="source">The source sequence.</param>
        /// <returns>The expression that forwards and caches value.</returns>
        public static ReactiveExpression<T> ToReactiveExpression<T>(this IObservable<T> source)
        {
            var ret = new ReactiveExpression<T>(source);
            return ret;
        }

        /// <summary>
        /// Converts an observable sequence to <see cref="ReactiveExpression{T}"/> with the specified value.
        /// </summary>
        /// <typeparam name="T">The type of value.</typeparam>
        /// <param name="source">The source sequence.</param>
        /// <param name="value">The initial value.</param>
        /// <returns>The expression that forwards and caches value.</returns>
        public static ReactiveExpression<T> ToReactiveExpression<T>(this IObservable<T> source, T value)
        {
            var ret = new ReactiveExpression<T>(source, value);
            return ret;
        }
    }

    /// <summary>
    /// Represents a read only value and an observable sequence. 
    /// </summary>
    /// <typeparam name="T">The type of value.</typeparam>
    public class ReactiveExpression<T> : IReactiveValue<T>
    {
        private readonly IObservable<T> source;
        private readonly BehaviorSubject<T> subject;
        private readonly IObservable<T> sink;
        private readonly CompositeDisposable subscriptions;

        /// <summary>
        /// Initializes a new instance of the ReactiveExpression&lt;T&gt; class with the specified observable sequence.
        /// </summary>
        /// <param name="source">The source sequence.</param>
        public ReactiveExpression(IObservable<T> source)
            : this(source, default(T))
        {
        }

        /// <summary>
        /// Initializes a new instance of the ReactiveExpression&lt;T&gt; class with the specified observable sequence and the specified value.
        /// </summary>
        /// <param name="source">The source sequence.</param>
        /// <param name="value">The initial value.</param>
        public ReactiveExpression(IObservable<T> source, T value)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            this.source = source;
            this.subject = new BehaviorSubject<T>(value);
            this.sink = subject.DistinctUntilChanged();

            this.subscriptions = new CompositeDisposable();
            this.subscriptions.Add(source.Subscribe(x => Value = x));
            this.subscriptions.Add(subject);
        }

        /// <summary>
        /// Gets the current value.
        /// </summary>
        public T Value
        {
            get
            {
                subscriptions.ThrowIfDisposed();

                var ret = sink.ToEnumerable().First();
                return ret;
            }
            private set
            {
                subscriptions.ThrowIfDisposed();

                subject.OnNext(value);
            }
        }

        public IDisposable Subscribe(IObserver<T> observer)
        {
            subscriptions.ThrowIfDisposed();

            var ret = sink.Subscribe(observer);
            return ret;
        }

        public void Dispose()
        {
            subscriptions.Dispose();
        }

        public override string ToString()
        {
            var ret = Value.ToString();
            return ret;
        }
    }
}
